<?php
/**
 * > 装備モデルマッパー
 *
 * 装備モデルに対し、
 * データの格納や更新を行うクラス。
 *
 * LICENSE: Miracle Maple by Mirage is licensed
 * under a Creative Commons 表示-非営利-継承 2.1 日本 License.
 *
 * @category   equip
 * @package    maple-story.jp
 * @version    8.0.0
 * @link       http://maple-story.jp/
 * @since      File available since Release 8.0.0
 * @copyright  Copyright 2011 Miracle Maple
 * @license    http://creativecommons.org/licenses/by-nc-sa/2.1/jp/     Creative Commons BY-NC-SA
 */
class Model_Mapper_TrnEquipment extends Model_Mapper_MapperAbstract
{
	public function search($options)
	{
		if (!is_array($options)) {
			throw new Exception('Invalid parameter has provided.');
		}
		
		$select = $this->getDbTable()->select();
		$select->where("display_flg = 1")
		       ->where("delete_flg = 0")
		       ->where("cash_flg = 0")
		       ->where("name IS NOT NULL");
		
		// 名前
		if (isset($options['name'])) {
			$select->where('name = ?', $options['name']);
		}
		
		// キーワード
		if (isset($options['keyword'])) {
			$select->where('name LIKE ?', '%' . $options['keyword'] . '%');
		}
		
		// 装備分類
		if (isset($options['groupCd'])) {
			$select->where("group_cd IN (?)", explode(",", $options['groupCd']));
		}
		
		// レベル
		if (isset($options['lv'])) {
			if (strpos($options['lv'], '-') === false) {
				$select->where('req_level = ?', $options['lv']);
			} else {
				$level = explode('-', $options['lv']);
				if ($level[0] == '') {
					$level[0] = -1;
				}
				if ($level[1] == '') {
					$level[1] = 999;
				}
				$select->where('req_level >= ?', $level[0])
				       ->where('req_level <= ?', $level[1]);
			}
		}
	
		// 表示順序
		if (isset($options['order'])
			&& is_array($options['order'])
		) {
			$select->order($options['order']);
		} else {
			$select->order(array('req_level ASC', 'id ASC'));
		}

		return $this->fetchAll($select);
	}
}
